﻿using BookStore.Solution.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStore.Solution.BLL
{
    public class BookManager
    {
        private string result = "";

        private DataTable generateTable()
        {
            DataTable dtBook = new DataTable();
            dtBook.Columns.Add("ProductID", typeof(string));
            dtBook.Columns.Add("ProductName", typeof(string));
            dtBook.Columns.Add("AuthorID", typeof(ICollection<Author>));
            dtBook.Columns.Add("PublisherID", typeof(string));
            dtBook.Columns.Add("YearPublication", typeof(string));
            dtBook.Columns.Add("Version", typeof(string));
            dtBook.Columns.Add("CategoryID", typeof(string));
            dtBook.Columns.Add("PageNumber", typeof(string));
            dtBook.Columns.Add("Description", typeof(string));
            dtBook.Columns.Add("Unit", typeof(string));
            dtBook.Columns.Add("UnitPrice", typeof(string));
            dtBook.Columns.Add("Quantity", typeof(string));
            dtBook.Columns.Add("Picture", typeof(string));
            return dtBook;


        }

        public List<Product> GetAll()
        {
            List<Product> product = new List<Product>();
            using (var context = new BookStoreEntities())
            {
                context.Configuration.LazyLoadingEnabled = false;
                product = (from a in context.Products
                           where a.Type == "Sách" && a.Status != "Đã xóa"
                           select a).ToList<Product>();
                foreach (Product p in product)
                {
                    context.Entry(p).Collection(pr => pr.Authors).Load();
                }
            }
            return product;
        }

        public Product GetbyID(string productID)
        {
            Product product = new Product();
            using (var context = new BookStoreEntities())
            {
                context.Configuration.LazyLoadingEnabled = false;
                product = (from a in context.Products
                           where a.ProductID == productID && a.Type == "Sách"
                           select a).First<Product>();
                context.Entry(product).Collection(p => p.Authors).Load();
            }
            return product;
        }

        public string Insert(Product product)
        {
            using (var context = new BookStoreEntities())
            {
                int numOfRows = context.Products.Where(p => p.ProductID == product.ProductID).Count();
                if (numOfRows == 0)
                {
                    // context.Products.Add(product);
                    context.Products.Attach(product);
                    context.Entry(product).State = System.Data.Entity.EntityState.Added;
                    if (context.SaveChanges() > 0)
                    {
                        SystemLogManager.Insert("Thêm sách: " + product);
                        result = "Thêm thành công";
                    }
                    else
                        result = "Thêm thất bại";

                }
                else
                {
                    result = "Mã sản phẩm đã tồn tại trong hệ thống. Vui lòng chọn mã sản phẩm khác.";
                }
            }
            return result;
        }

        public string Update(Product product)
        {
            using (var context = new BookStoreEntities())
            {

                Product nProduct = (from p in context.Products
                                    where p.ProductID == product.ProductID
                                    select p).First();

                Author[] authors = new Author[product.Authors.Count];
                product.Authors.CopyTo(authors, 0);
                nProduct.Authors.Clear();
                foreach (Author a in authors)
                    nProduct.Authors.Add(context.Authors.Where(s => s.AuthorID == a.AuthorID).FirstOrDefault<Author>());
                nProduct.ProductName = product.ProductName;
                nProduct.PublisherID = product.PublisherID;
                nProduct.YearPublication = product.YearPublication;
                nProduct.Version = product.Version;
                nProduct.CategoryID = product.CategoryID;
                nProduct.PageNumber = product.PageNumber;
                nProduct.Decription = product.Decription;
                nProduct.Unit = product.Unit;
                nProduct.UnitPrice = product.UnitPrice;
                nProduct.Quatity = nProduct.Quatity;
                nProduct.Picture = product.Picture;
                nProduct.Status = product.Status;
                if (context.SaveChanges() > 0)
                {
                    SystemLogManager.Insert("Cập nhật sách: " + product);
                    result = "Cập nhật thông tin sách thành công";
                }
                else
                    result = "Cập nhật thông tin sách thất bại";
            }
            return result;
        }

        public int Delete(string ProductID)
        {
            using (var context = new BookStoreEntities())
            {
                Product nProduct = (from p in context.Products
                                    where p.ProductID == ProductID
                                    select p).First();
                nProduct.Status = "Đã xóa";
                int result = context.SaveChanges();
                if (result > 0)
                {
                    SystemLogManager.Insert("Xóa sách: " + nProduct);
                }
                return result;
            }
        }


        public List<Product> SearchbyID(string productID)
        {
            List<Product> product = new List<Product>();
            using (var context = new BookStoreEntities())
            {
                context.Configuration.LazyLoadingEnabled = false;
                product = (from a in context.Products
                           where a.Type == "Sách" && a.ProductID == productID && a.Status != "Đã xóa"
                           select a).ToList<Product>();
                foreach (Product p in product)
                {
                    context.Entry(p).Collection(pr => pr.Authors).Load();
                }
            }
            return product;
        }

        public List<Product> SearchbyName(string productName)
        {
            List<Product> product = new List<Product>();
            using (var context = new BookStoreEntities())
            {
                context.Configuration.LazyLoadingEnabled = false;
                product = (from a in context.Products
                           where a.Type == "Sách" && a.ProductName.Contains(productName) && a.Status != "Đã xóa"
                           select a).ToList<Product>();
                foreach (Product p in product)
                {
                    context.Entry(p).Collection(pr => pr.Authors).Load();
                }
            }
            return product;
        }

        private bool SearchAuthor(Author[] authors, Author entity)
        {
            foreach (Author a in authors)
                if (a.AuthorID == entity.AuthorID)
                    return true;
            return false;
        }

        public List<Product> SearchbyAuthor(Author author)
        {
            List<Product> product = new List<Product>();
            List<Product> result = new List<Product>();
            using (var context = new BookStoreEntities())
            {
                product = GetAll();
                foreach (Product p in product)
                {
                    Author[] authors = new Author[p.Authors.Count];
                    p.Authors.CopyTo(authors, 0);
                    if (SearchAuthor(authors, author))
                    {
                        result.Add(p);
                    }
                }

            }
            return result;
        }

        public List<Product> SearchbyPublisher(string publisherID)
        {
            List<Product> product = new List<Product>();
            using (var context = new BookStoreEntities())
            {
                product = (from p in context.Products
                           where p.PublisherID == publisherID && p.Type == "Sách" && p.Status != "Đã xóa"
                           select p).ToList<Product>();
                foreach (Product p in product)
                {
                    context.Entry(p).Collection(pr => pr.Authors).Load();
                }
            }
            return product;
        }

        public List<Product> SearchbyCategory(string categoryID)
        {
            List<Product> product = new List<Product>();
            using (var context = new BookStoreEntities())
            {
                product = (from p in context.Products
                           where p.CategoryID == categoryID && p.Type == "Sách" && p.Status != "Đã xóa"
                           select p).ToList<Product>();
                foreach (Product p in product)
                {
                    context.Entry(p).Collection(pr => pr.Authors).Load();
                }
            }
            return product;
        }

    }
}
